const { getFilterRulerListByGroupId,deleteFilterRulerById,addFilterRuler,isExistsFilterRulerInSpecGroup,modifyFilterRuler,getAllFilterRulerList} = require("../model/filterRuler");
const { responsSuccess, responsError } = require("../utils/responseUtil");
const {DuplicateValueError}=require('../commonError/logicError')
const {validateParams} = require("../utils/validateReqParam");
module.exports.getFilterRulerListByGroupIdController=async function(request, response){
    getFilterRulerListByGroupId(request.params['groupId']).then(filterRulerList=>{
        responsSuccess(response, filterRulerList)
    }).catch(err => {
        console.log('getFilterRulerListByGroupIdController --> err', err)
        responsError(response, err)
    })
}

module.exports.addFilterRulerController=async function(request, response){
    validateParams(request.body, {
        'groupId': { required: true,type: 'number', default:null },
        'filterRuler': { required: true,type: 'string', default:null }
    }).then(async reqData=>{
        //同组内的filterRuler不能重复
        const [{count}]=await isExistsFilterRulerInSpecGroup(reqData['groupId'],reqData['filterRuler']).catch(err => {
            console.log('getAllTeamListController --> err', err)
            responsError(response, err)
        })
        if (count>0){
            responsError(response, new DuplicateValueError(),'本组内已存在该过滤规则')
            return
        }
        //插入数据
        addFilterRuler(reqData).then(()=>{
            responsSuccess(response, null)
        })
    }).catch(err => {
            console.log('addFilterRulerController --> err', err)
            responsError(response, err)
    })

}

module.exports.modifyFilterRulerController=async function(request, response){
    validateParams(request.body, {
        'rid': { required: true,type: 'number', default:null },
        'groupId': { required: true,type: 'number', default:null },
        'filterRuler': { required: true,type: 'string', default:null }
    }).then(async reqData=>{
        await modifyFilterRuler(reqData).then(()=>{
            responsSuccess(response,null)
        })
    }).catch(err => {
        console.log('modifyFilterRulerController --> err', err)
        responsError(response, err)
    })

}

module.exports.deleteFilterRulerController=async function(request, response){
    deleteFilterRulerById(request.params['filterRulerId']).then(()=>{
        responsSuccess(response, null)
    }).catch(err => {
        console.log('deleteFilterRulerController --> err', err)
        responsError(response, err)
    })
}
